package com.hgsoft.yfzx.component.filehelper.util;

import com.hgsoft.yfzx.component.filehelper.entity.Computer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.io.*;
import java.nio.charset.Charset;

/**
 * description
 *
 * @author RuiHaoZ on hgits.cn
 * @date 2018/12/29 16:54
 */
public class CmdDocUtil {

    private static Logger logger = LoggerFactory.getLogger(CmdDocUtil.class);

    public static void main(String[] args) throws IOException {
        Computer computer = new Computer("10.173.234.116","Administrator","hgrica1@");
        taskKillByName(computer,"FileClient.exe");
    }

    /**
     * 执行cmd脚本，无返回
     *
     * @param execute
     * @throws IOException
     */
    public static void executeNoReturn(String execute) throws IOException {
        Runtime.getRuntime().exec(execute);
    }

    /**
     * 执行exe文件，会弹出窗口
     *
     * @param exePathXF
     * @throws IOException
     */
    public static void openDeskTopExe(String exePathXF) throws IOException {
        Desktop.getDesktop().open(new File(exePathXF));
    }

    /**
     * 根据进程名称杀掉远程电脑进程
     *
     * @param computer
     * @param exeName
     * @throws IOException
     */
    public static void taskKillByName(Computer computer, String exeName) throws IOException {
        StringBuffer command = new StringBuffer("taskkill");
        if (computer != null) {
            command.append(" /s ").append(computer.getIpAddress())
                    .append(" /u ").append(computer.getUsername())
                    .append(" /p ").append(computer.getPassword());
        }
        command.append(" /f /im");
        command.append(" ");
        command.append(exeName);
        execute(command.toString());
    }

    /**
     * 根据进程名称杀掉进程
     *
     * @param exeName
     * @throws IOException
     */
    public static void taskKillByName(String exeName) throws IOException {
        taskKillByName(null, exeName);
    }

    /**
     * 查看本地进程列表
     *
     * @return
     * @throws IOException
     */
    public static String taskListEXE() throws IOException {
        return taskListEXE(null);
    }

    /**
     * 查看远程电脑进程列表
     *
     * @param computer
     * @return
     * @throws IOException
     */
    public static String taskListEXE(Computer computer) throws IOException {
        StringBuffer command = new StringBuffer("tasklist");
        if (computer != null) {
            command.append(" /s ").append(computer.getIpAddress())
                    .append(" /u ").append(computer.getUsername())
                    .append(" /p ").append(computer.getPassword());
        }
        return execute(command.toString());
    }

    /**
     * 执行cmd语句
     *
     * @throws IOException
     */
    public static String execute(String exec) throws IOException {
        // 调用外部程序
        Process process = Runtime.getRuntime().exec(exec);
        final InputStream in = process.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(in, Charset.forName("GBK")));
        StringBuilder buf = new StringBuilder();
        String line = null;
        while ((line = br.readLine()) != null) {
            buf.append(line);
            buf.append("\n");
        }
        logger.debug("执行：{} \n输出结果为：{}", buf);
        return buf.toString();
    }

    public static boolean isExist(String procName) throws IOException {
        return isExist(null, procName);
    }

    /**
     * 查看对应进程是否存在
     *
     * @param computer
     * @param procName
     * @return
     * @throws IOException
     */
    public static Boolean isExist(Computer computer, String procName) throws IOException {
        Boolean existProc = false;
        BufferedReader bufferedReader = null;
        try {
            StringBuffer command = new StringBuffer("tasklist");
            if (computer != null) {
                command.append(" /s ").append(computer.getIpAddress())
                        .append(" /u ").append(computer.getUsername())
                        .append(" /p ").append(computer.getPassword());
            } else {
                command.append(" -fi ").append('"').append("imagename eq ").append(procName).append('"');
            }
            Process proc = Runtime.getRuntime().exec(command.toString());
            bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains(procName)) {
                    existProc = true;
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return existProc;
    }
}
